<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ include file="/jsp/common/common.jsp"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>供货商订单详细页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link rel="stylesheet" type="text/css"
    href="<c:url value="/static/jquery-easyui/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
    href="<c:url value="/static/jquery-easyui/themes/icon.css"/>">

<link rel="stylesheet" type="text/css"
    href="<c:url value="/static/css2/css/common.css"/>">
<link rel="stylesheet" type="text/css"
    href="<c:url value="/static/css2/fonts/iconfont.css"/>">
<link rel="stylesheet" type="text/css"
    href="<c:url value="/static/css2/css/style.css"/>">
<link rel="stylesheet" type="text/css"
    href="<c:url value="/static/jquery-easyui/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
    href="<c:url value="/static/jquery-easyui/themes/icon.css"/>">
<link rel="Stylesheet" type="text/css"
    href="<c:url value="/static/css/jquery.ui.css"/>" />

<script type="text/javascript"
    src="<c:url value="/static/jquery-easyui/jquery-1.6.min.js"/>"></script>
<script type="text/javascript"
    src="<c:url value="/static/jquery-easyui/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
    src="<c:url value="/static/jquery-easyui/plugins/jquery.panel.js"/>"></script>
<script type="text/javascript"
    src="<c:url value="/static/jquery-easyui/plugins/jquery.datagrid.js"/>"></script>
<script type="text/javascript"
    src="<c:url value="/static/jquery-easyui/plugins/jquery.messager.js"/>"></script>
<script type="text/javascript"
    src="<c:url value="/static/jquery-easyui/ext-for-framework.js.jsp"/>"></script>
<script type="text/javascript"
    src="<c:url value="/static/jquery-easyui/locale/easyui-lang-zh_CN.js"/>"></script>
<script type="text/javascript"
    src="<c:url value="/static/js/common.js"/>"></script>
<script type="text/javascript"
    src="<c:url value="/static/js/LodopFuncs.js"/>"></script>

<script type="text/javascript"
    src="<c:url value="/static/js/footer.js"/>"></script>
<script type="text/javascript"
    src="<c:url value="/static/js/showMsg.js"/>"></script>
<script type="text/javascript"
    src="<c:url value="/static/listBox/jquery.clever.listbox.js"/>"></script>
<script type="text/javascript"
    src="<c:url value="/jsp/ordermanage/customDetailedMaterIndexForSup.js"/>"></script>
<script type="text/javascript">
    var dataLeft;
    var xhrDelete;
    function initList(listDisplayEId) {
        $('#' + listDisplayEId)
                .datagrid(
                        {
                            nowrap : false,
                            striped : true,
                            collapsible : true,
                            remoteSort : true,
                            notSelectByRow : false,
                            toolbar : [],
                            onLoadError : function() {
                                $.messager.alert('错误', '获取数据失败，请与管理员联系！', 'error');
                            },
                            onClickCell : onClickCell,
                            toolbar : [
                                    {
                                        id : listDisplayEId + '_btn_del',
                                        text : '删除',
                                        iconCls : 'icon-cancel',
                                        handler : function() {
                                            var row = $('#' + listDisplayEId)
                                                    .datagrid('getSelections');
                                            if (row.length == 0) {
                                                $.messager.alert('提示',
                                                        '请选择一条记录操作！', 'info');
                                                return;
                                            } else {
                                                $.messager.confirm( '提示', '是否删除该条记录？',
                                                    function(r) {
                                                        if (r) {
                                                            deleteMater(row);
                                                        }
                                                });
                                            }
                                        }
                                    },
                                    {
                                        id : listDisplayEId + '_btn_add',
                                        text : '临时加货',
                                        iconCls : 'icon-add',
                                        handler : function() {
                                         
                                            if ("${OrderDistributorPo.ordStateDistributor }" ==3) {
                                            	alert("订单已被确认！请新建订单！");
                                            } else {
                                                var new_win = window.open(
                                                                "<c:url value='/jsp/openIndexSun.jsp?url=/distributor/preUpdateSelectMaterForDistributor.do'/>"
                                                                        + "?ordId=${OrderDistributorPo.ordId }"
                                                                        + "&supId=${OrderDistributorPo.ordSupId }",
                                                                "sun",
                                                                "width=1200px,height=600px,status=no,scrollbars=yes");
                                                new_win.focus();
                                            }
                                        }
                                    },
                                    {
                                        id : listDisplayEId + '_btn_add',
                                        text : '根据日期和订单转换订单材料（带摊位）',
                                        iconCls : 'icon-add',
                                        handler : function() {
                                            //先检查订单
                                            
                                            var row = $('#' + listDisplayEId).datagrid('getSelections');
                                            if (row.length < 1) {
                                                $.messager.alert('提示','请选择一条记录操作！', 'info');
                                                return;
                                            } else {
                                                changeOrdMater(row);
                                            }
                                        }
                                    },
                                    {
                                        id : listDisplayEId + '_btn_add',
                                        text : '修改数量和价格',
                                        iconCls : 'icon-add',
                                        handler : function() {
                                            updateMaterWeightAndValue();
                                        }
                                    } ]
                        });
    };
    $(document).ready(function() {
        initList('listDisplay');
    });
    

    var editIndex = undefined;
    $.extend($.fn.datagrid.methods, {
        editCell: function(jq,param){
            return jq.each(function(){
                //取得周期标记
                var periodValueFlag =  $("#listDisplay").datagrid('getData').rows[param.index]["periodValueFlag"];
                
                var opts = $(this).datagrid('options');
                var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
                for(var i=0; i<fields.length; i++){
                    var col = $(this).datagrid('getColumnOption', fields[i]);
                    col.editor1 = col.editor;
                    if (fields[i] != param.field){
                        col.editor = null;
                    }
                    
                    //如果周期报价不能编辑
                    /* if( periodValueFlag ==1 && param.field == 'ordMaterValueRealUnit'){
                        col.editor = null;
                    } */
                    //如果订单已被确认则不能编辑
                    if( "${OrderDistributorPo.ordStateDistributor }" ==3){
                        col.editor = null;
                    }
                }
                $(this).datagrid('beginEdit', param.index);
                for(var i=0; i<fields.length; i++){
                    var col = $(this).datagrid('getColumnOption', fields[i]);
                    col.editor = col.editor1;
                }
            });
        }
    });
    function endEditing(){
        if (editIndex == undefined){return true}
        if ($('#listDisplay').datagrid('validateRow', editIndex)){
            $('#listDisplay').datagrid('endEdit', editIndex);
            editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }
    function onClickCell(index, field){
        if (endEditing()){
            $('#listDisplay').datagrid('selectRow', index)
                    .datagrid('editCell', {index:index,field:field});
            editIndex = index;
        }
    }
    //导入数量
    function updateMaterWeightAndValue(){
        var rows = $('#listDisplay').datagrid('getSelections');
        if(rows.length == 0){
            $.messager.alert('提示','请选择一条记录操作！','info');
            return;
        } else {
            //生成json数据
            var materOrderList = new Array();
            var jsonFater;
            for (var i = 0; i < rows.length; i++) {
                var rowIndex = $('#listDisplay').datagrid('getRowIndex',
                        rows[i]['ordDistributorMaterId']);
                $('#listDisplay').datagrid('endEdit', rowIndex);
                
                
                var jsonSun = "{ordMaterId:'" + rows[i]['ordMaterId'] 
                        + "',ordMaterLevel:'" + rows[i]['ordMaterLevel']
                        + "',ordDistributorMaterId:'" + rows[i]['ordDistributorMaterId'] 
                        + "',ordMaterWeightReality:'" + rows[i]['ordMaterWeightReality'] 
                        + "',ordMaterValueUnit:'" + rows[i]['ordMaterValueUnit'] + "'}";
                materOrderList.push(jsonSun);
                
            }
            //beginEditList(rows, '#listDisplay', 'ordSupMaterId');

            jsonFater = JSON.stringify(materOrderList);
            var form = new FormData();
            //ajax 提交表单数据
            form.append("ordId", "${OrderDistributorPo.ordId }"); // 可以增加表单数据
            form.append("ordSupId", "${OrderDistributorPo.ordSupId }"); // 可以增加表单数据
            form.append("materdategridList", jsonFater); // 可以增加表单数据
            // XMLHttpRequest 对象
            xhr = new XMLHttpRequest();
            xhr.open("post", '<c:url value='/distributor/updateMaterWeightAndMaterValueForDistributor.do'/>', true);
            xhr.onload = function(a) {
                $.messager.alert('系统提示', '修改成功', 'info', function() {
                    $('#'+listDisplayEId).datagrid('reload');
                    $('#'+listDisplayEId).datagrid('clearSelections');
                });
            }; 
            xhr.send(form);
        }
        
    }
    var xhrDelete;
    function deleteMater(row){
    	
        var dateone = '[ ';
        for(var i=0; i<row.length; i++){
            dateone = dateone+'{"ordMaterId":"'+ row[i]['ordMaterId']+'","ordDistributorMaterId":"'+ row[i]['ordDistributorMaterId']+'","ordMaterLevel":"'+ row[i]['ordMaterLevel'] +'"}';
            if(i != row.length-1){
                dateone = dateone+',';
            }
        }
        dateone = dateone + ']';
        var form = new FormData();
        form.append("ordId", "${OrderDistributorPo.ordId }"); // 可以增加表单数据
        form.append("ordSupId", "${OrderDistributorPo.ordSupId }"); // 可以增加表单数据
        form.append("materdategridList", dateone); // 可以增加表单数据
        xhrDelete = new XMLHttpRequest();
        xhrDelete.onreadystatechange = xhrDelete_return;
        xhrDelete.open("post", '<c:url value='/distributor/deleteDistributorOrderMater.do'/>', true);
        xhrDelete.send(form);
           
    }
    function xhrDelete_return() {
        if (xhrDelete.readyState == 4) {// 4 = "loaded"

            if (xhrDelete.status == 200) {// 200 = OK
                var data;
                data = xhrDelete.responseText;
                if (typeof (data) == 'string' && data != '') {
                    data = JSON.parse(data);
                }
                if (data.success) {
                    $.messager.alert("提示", "操作成功！", 'info');
                    $('#listDisplay').datagrid('clearSelections');

                    $('#listDisplay').datagrid('reload');
                } else {
                    $.messager.alert("提示", data.msg, 'info');
                }
            } else {
                alert("Problem retrieving XML data");
            }
        }
    }
    var xhrChange;
    function changeOrdMater(row){
    	
        for(var i=0; i<row.length; i++){
        	var form = new FormData();
            form.append("ordId", "${OrderDistributorPo.ordId }"); // 可以增加表单数据
            form.append("ordMaterId", row[i]['ordMaterId']); // 可以增加表单数据
            form.append("ordMaterValueLast", row[i]['ordMaterValueLast']); // 可以增加表单数据
            form.append("ordMaterLevel", row[i]['ordMaterLevel']); // 可以增加表单数据
            form.append("ordSupId", "${OrderDistributorPo.ordSupId }"); // 可以增加表单数据

            xhrChange = new XMLHttpRequest();
            xhrChange.onreadystatechange = xhrChange_return;
            xhrChange.open("post", '<c:url value='/distributor/changeOrdMater.do'/>', true);
            xhrChange.send(form);
        	
        }
        
    }
    function xhrChange_return(){
    	if (xhrChange.readyState == 4) {// 4 = "loaded"

            if (xhrChange.status == 200) {// 200 = OK
                var data;
                data = xhrChange.responseText;
                if (typeof (data) == 'string' && data != '') {
                    data = JSON.parse(data);
                }
                if (data.success) {
                    //$.messager.alert("提示", "操作成功！", 'info');
                    $('#listDisplay').datagrid('clearSelections');
                    $('#listDisplay').datagrid('reload');
                } else {
                    $.messager.alert("提示", data.msg, 'info');
                    $('#listDisplay').datagrid('clearSelections');
                    $('#listDisplay').datagrid('reload');
                }
            } else {
                alert("Problem retrieving XML data");
            }
        }
    }
    
    // 颜色
    function cellStylerTrue(value, row, index) {
    	if (row['ordMaterWeight'] != row['ordMaterWeightReality'] ) {
            return 'background-color:red;';
        }   
    }
 // 颜色
    function cellStylerValue(value, row, index) {
        if (row['ordMaterValueUnit'] != row['ordMaterValueLast'] ) {
            return 'background-color:red;';
        }   
    }
</script>
</head>

<body>
    <form id="AddFoodForm" name="AddFoodForm" method="post">
        <table cellpadding="5">
            <tr>
                <td><input class="easyui-validatebox" type="hidden"
                    name="ordId" required="true" validType="length[1,30]"
                    value="${OrderDistributorPo.ordId }" /> <input class="easyui-validatebox"
                    type="hidden" name="materdategridList" /> <input
                    class="easyui-validatebox" type="hidden" id="ordSupId"
                    name="ordSupId" value="${OrderDistributorPo.ordSupId }" />
                <td><input class="easyui-validatebox" editable="false"
                    id="supName" name="supName" type="hidden"></input></td>
            </tr>
        </table>
    </form>

    <table id="listDisplay" title="材料订单列表" singleSelect=false
        iconCls="icon-list" idField="ordDistributorMaterId"
        url="<c:url value="/distributor/findDistributorDetailOrder.do?ordId=${OrderDistributorPo.ordId }&ordSupId=${OrderDistributorPo.ordSupId }"/>"
        style="width: 100%;" loadMsg="请稍候......" pagination="false"
        pageSize="20" pageList="[10,20,30,40,50,100,150]" sortName="roleId"
        rownumbers="true" sortOrder="asc" showFooter="true">
        <thead>
            <tr>
                <th field="id" width="25" align="center" checkbox="true"></th>
                <th field="ordDistributorMaterId" width="80" sortable="true" hidden="true">ID</th>
                <th field="ordId" width="60" sortable="true">订单ID</th>
                <th field="ordSupId" width="60" sortable="true" hidden="true">供货商ID</th>
                <th field="ordSupName" width="80" sortable="true" hidden="true">供货商名</th>
                <th field="ordMaterId" width="60" sortable="true">材料id</th>
                <th field="materName" width="80" sortable="true">材料名</th>
                <th field="materType" width="80" sortable="true" formatter="formatFoodType1" >种类</th>
                <th field="ordMaterLevel" width="50" sortable="true" formatter="formatFoodType" hidden="true">品级</th>
                <th field="brands" width="60" sortable="true">品牌</th>
                <th field="specifications" width="60" sortable="true">规格</th>
                <th field="ordMaterWeight" width="60" sortable="true" styler="cellStylerTrue">需求数量</th>
                <th field="ordMaterWeightReality" width="60" sortable="true" editor="text" styler="cellStylerTrue">送货数量</th>
                <th field="unit" width="60" sortable="true">单位</th>
                <th field="ordMaterValueLast" width="70" sortable="true" styler="cellStylerValue">上次价格</th>
                <th field="ordMaterValueUnit" width="70" sortable="true" editor="text" styler="cellStylerValue">单价</th>
                <th field="ordMaterValue" width="70" sortable="true">总价</th>
                <th field="code" width="70" sortable="true">摊位</th>
            </tr>
        </thead>
    </table>
    <object id="LODOP_OB"
        classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0>
        <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0></embed>
    </object>
</body>

</html>